using UnityEngine;
using System.Collections;

public class lwCompatibilityTester : MonoBehaviour {

public enum CompatibilityError { powerfullIncompatibility, count };
	public string[] 	m_sErrorMsgArray = new string[(int)CompatibilityError.count];
	public string 		m_sAppTitleTxtId ="";
	public string		m_sQuitTxtId = "";
	public Font			m_Font;
	public int 			m_FontSize = 10;
	
	private CompatibilityError m_error = CompatibilityError.count;
	
	
	public MonoBehaviour scriptWithMethodToStartGame;
	public string methodToStartGame = "";
	
	public MonoBehaviour scriptWithMethodToStopGame;
	public string methodToStopGame = "";
	
	// Use this for initialization
	void Awake () 
	{
		LaunchTest();
	}
	
	// ----------------------------------------------------------------------
	// Created PC : 22/01/13
	// ----------------------------------------------------------------------
	void LaunchTest()
	{
		if( !IsEnoughPowerfull() )
			m_error = CompatibilityError.powerfullIncompatibility;
		
		if ( m_error != CompatibilityError.count ) 
		{ 
			if (scriptWithMethodToStopGame != null)
				scriptWithMethodToStopGame.Invoke(methodToStopGame, 0);
		}
		
		if ( m_error == CompatibilityError.count ) 
		{ 
			if (scriptWithMethodToStartGame != null)
				scriptWithMethodToStartGame.Invoke(methodToStartGame, 0);
		}
	}
	
	// ----------------------------------------------------------------------
	// Created PC : 22/01/13
	// ----------------------------------------------------------------------
	bool IsEnoughPowerfull()
	{
#if UNITY_IPHONE && !UNITY_EDITOR
		if( iPhoneSettings.generation <= iPhoneGeneration.iPhone3GS ||
				iPhoneSettings.generation == iPhoneGeneration.iPodTouch4Gen )
		{
			return false;
		}
#endif
		return true;
	}
	
	// ----------------------------------------------------------------------
	// Created PC : 22/01/13
	// ----------------------------------------------------------------------
	void OnGUI() 
	{ 
		if ( m_error != CompatibilityError.count ) 
		{ 
			string sTxt = string.Empty;
			lwLangManager Lg = lwLangManager.GetInstance();
			if( m_sErrorMsgArray != null && m_sErrorMsgArray.Length > (int)m_error )
			{
				sTxt = Lg.GetString( m_sErrorMsgArray[(int)m_error] );
				sTxt = sTxt.Replace( "%TITLE%", Lg.GetString( m_sAppTitleTxtId ) );
			}
			
			if( string.IsNullOrEmpty( sTxt ) )
				sTxt = "Error the application can't be launch";
			
			GUIStyle style = new GUIStyle( GUI.skin.label );
			GUIStyle BtnStyle = new GUIStyle( GUI.skin.button );
			
			Vector2 vBtnSize = new Vector2( 200f, 40f );
			if( m_Font != null )
			{
				style.font = m_Font;
				style.fontSize = m_FontSize;
				
				BtnStyle.font = m_Font;
				BtnStyle.fontSize = m_FontSize;
				style.alignment = TextAnchor.MiddleCenter;
				
				vBtnSize = new Vector2( m_FontSize * 10, m_FontSize * 2 );
			}
			
			string sQuitTxt = Lg.GetString(m_sQuitTxtId);
			if (string.IsNullOrEmpty( sQuitTxt ) )
				sQuitTxt = "Quit";
			
			GUI.Label(new Rect((float)50, 0, Screen.width - 100, Screen.height * 2f / 3f), sTxt, style ); 
			if ( GUI.Button( new Rect((float)Screen.width/2 - (vBtnSize.x/2), Screen.height*3/4, vBtnSize.x, vBtnSize.y), sQuitTxt, BtnStyle ) ) 
			    Application.Quit(); 
		} 
    }
}
